<?php
require ('common.inc.php');
        require_once('ethereum/Ethereum.php');
        $CoinClient = new EthClient('172.22.108.36', 18554);
        $json = $CoinClient->eth_blockNumber(true);
        if (empty($json) || $json <= 0) {
            exit("钱包对接失败");
        }
        require_once('ethereum/dbc.php');
        $dba = new Dbc($GLOBALARR['dbhost'], $GLOBALARR['dbuser'],$GLOBALARR['dbpwd'], 'utf8', $GLOBALARR['dbname']);

        $coin = 'vic_address';
        //开始轮询
        $listtransactions = $CoinClient->listLocal($json);
        echo 'listtransactions:' . count($listtransactions) . "\n";
        if (empty($listtransactions)) {
            exit('高度太高，无法轮询。');
        }
        foreach ($listtransactions as $trans) {
            if (!$trans->input) {
                echo 'empty to continue' . "<br>";
                continue;
            }
            $to = $trans->input;
            if (strlen($to) == 138) {
                $value = substr($trans->input, 74, 64);
                $to = "0x" . substr($to, 34, 40);
                //转入
                $user = $dba->select_all(" select * from ld_user_account where vic_address = " . "'$to'");
                if ($user) {
                    if ($trans->to == '0xcf6047999185a7a3f953282b392abbc27d93133b') {

                        $user_name = $user['uname'];
                        $is_txid = $dba->select_all("select * from ld_myzr WHERE username = "."'$user_name'");
                        if ($is_txid) {
                            echo 'txid had found continue' . "<br>";
                            continue;
                        }
                        echo 'start receive do:' . "<br>";
                        $sfee = 0;
                        $final_amount = $CoinClient->real_banlance_token($CoinClient->decode_hex($value), '8');

                        $rs = [];

                        $myzr_number = $user['cash_purse'] + $final_amount;
                        $re[] = $dba->update(['cash_purse' =>$myzr_number ], 'ld_user_account' , 'uname = ' ."'$user_name'");

                        $data['username'] = $user_name;
                        $data['hash'] = $trans->hash;
                        $data['qb_address'] = $to;
                        $data['in_number'] = $final_amount;
                        $data['addtime'] = time();
                        $add_data = $db->insert($data, 'ld_myzr');
                        $i = 3;
                        while($i>0){
                            if(!$add_data){
                                $i--;
                            }else{
                                break;
                            }
                        }
                    }
                }
            }
        }

?>
